package com.example.liunanxin.lnx226ordering.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.liunanxin.lnx226ordering.R;
import com.example.liunanxin.lnx226ordering.adapter.FoodCommentsAdapter;
import com.example.liunanxin.lnx226ordering.beans.FoodBean;
import com.example.liunanxin.lnx226ordering.beans.FoodCommentsBean;
import com.example.liunanxin.lnx226ordering.beans.UserBean;
import com.example.liunanxin.lnx226ordering.iface.DoCollectfoodListener;
import com.example.liunanxin.lnx226ordering.iface.FoodCommentsListener;
import com.example.liunanxin.lnx226ordering.iface.FoodDetailListener;
import com.example.liunanxin.lnx226ordering.iface.IsCollectListener;
import com.example.liunanxin.lnx226ordering.model.DoCollectfoodModel;
import com.example.liunanxin.lnx226ordering.model.FoodCommentsModel;
import com.example.liunanxin.lnx226ordering.model.FoodDetailModel;
import com.example.liunanxin.lnx226ordering.model.IsCollectModel;
import com.example.liunanxin.lnx226ordering.url.Url;
import com.squareup.picasso.Picasso;

import java.util.List;

public class FoodDetailActivity extends AppCompatActivity {
    TextView tv_foodname,tv_foodprice,tv_foodintro,tv_collect;
    ImageView iv_foodpic,iv_collect;
    private int food_id,user_id;
    private FoodCommentsAdapter foodCommentsAdapter;
    private RecyclerView recyclerView;
    private FoodBean foodBeans;
    private List<FoodCommentsBean> foodCommentsBeans;
    private Boolean isCollect;
    private SharedPreferences sharedPreferences;
//    //定义水平进度条
//    private ProgressBar horizonP;
//    //定义圆形进度条
//    private ProgressBar circleP;
//    //完成进度
//    private int mProcessStatus = 0;
//    //声明一个用于处理消息的Handler类的对象
//    private Handler mHandler;
//




    private FoodCommentsListener foodCommentsListener=new FoodCommentsListener() {
        @Override
        public void onResponse(List<FoodCommentsBean> foodCommentsBean) {
            foodCommentsBeans=foodCommentsBean;
            if (foodCommentsBeans==null){
                Toast.makeText(FoodDetailActivity.this,"null",Toast.LENGTH_LONG).show();
            }
            else{
                foodCommentsAdapter.setData(foodCommentsBeans);
            }
        }

        @Override
        public void onFail(String mag) {
            Toast.makeText(FoodDetailActivity.this,mag,Toast.LENGTH_LONG).show();

        }
    };
    private FoodDetailListener foodDetailListener=new FoodDetailListener() {
        @Override
        public void onResponse(FoodBean foodBean) {
            foodBeans=foodBean;

            tv_foodname.setText(foodBeans.getFoodname());
            tv_foodintro.setText("简介： "+foodBeans.getIntro());
            tv_foodprice.setText("￥ "+foodBeans.getPrice()+"元");
            Picasso.get().load(Url.USERURL+foodBeans.getPic()).into(iv_foodpic);
        }

        @Override
        public void onFail(String mag) {
            Toast.makeText(FoodDetailActivity.this,mag,Toast.LENGTH_LONG).show();
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);

//        /**
//         * 在主活动onCreate方法中，首先获得水平进度条和圆形进度条，
//         * 然后通过匿名内部类实例化处理消息的Handler类的对象，并重写其handlerMessage方法，
//         * 实现当好吃操作没有完成时更新进度，否则设置进度条不显示。
//         * @param savedInstanceState
//         */
//
//
//            horizonP = (ProgressBar) findViewById(R.id.progressBar1);
//
//            mHandler = new Handler(){
//                @Override
//                public void handleMessage(Message msg) {
//                    if (msg.what == 0x111) {
//                        horizonP.setProgress(mProcessStatus);//更新进度
//                    } else {
//                        Toast.makeText(FoodDetailActivity.this,"耗时操作已经完成",Toast.LENGTH_SHORT).show();
//                        horizonP.setVisibility(View.GONE);
//                        circleP.setVisibility(View.GONE);
//                    }
//                }
//            };
//
//            //开个线程用语模拟耗时操作
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    while (true) {
//                        mProcessStatus = doWork();//获取耗时操作完成的百分比
//                        Message msg = new Message();
//                        if (mProcessStatus<100) {
//                            msg.what = 0x111;
//                            mHandler.sendMessage(msg);//发送消息
//                        } else {
//                            msg.what = 0x110;
//                            mHandler.sendMessage(msg);//发送消息
//                            break;
//                        }
//                    }
//                }
//                //模拟一个耗时操作
//                private int doWork() {
//                    mProcessStatus += Math.random()*10;//改变完成进度
//                    try {
//                        Thread.sleep(200);//每隔200毫秒进度改变一次
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    return mProcessStatus;//返回新的进度
//                }
//            }).start();//开启一个新的线程







    tv_foodname=findViewById(R.id.textView15);
        tv_foodintro=findViewById(R.id.textView16);
        tv_foodprice=findViewById(R.id.textView17);
        iv_foodpic=findViewById(R.id.imageView2);
        iv_collect=findViewById(R.id.imageView13);
        tv_collect=findViewById(R.id.textView20);



        food_id=getIntent().getIntExtra("food_id",0);

        sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
         user_id=Integer.parseInt(sharedPreferences.getString("userid","获取userid失败"));

       FoodDetailModel foodDetailModel=new FoodDetailModel();
       foodDetailModel.getFoodById(food_id,foodDetailListener);


        initRecyclerView();
        FoodCommentsModel foodCommentsModel=new FoodCommentsModel();
       foodCommentsModel.getFoodCommentsResult(food_id,foodCommentsListener);

        isCollect();

        iv_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doCollect();
            }
        });
    }

    public void initRecyclerView(){
        recyclerView=findViewById(R.id.recyclerView_food_comment);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        foodCommentsAdapter=new FoodCommentsAdapter();
       recyclerView.setAdapter(foodCommentsAdapter);

    }

    private void isCollect(){
        IsCollectModel isCollectModel=new IsCollectModel();
        isCollectModel.isCollect(user_id,food_id,1,isCollectListener);
    }

    private void doCollect(){
        DoCollectfoodModel doCollectfoodModel =new DoCollectfoodModel();
        doCollectfoodModel.doCollect(user_id,food_id, doCollectfoodListener);
    }

    private DoCollectfoodListener doCollectfoodListener =new DoCollectfoodListener() {
        @Override
        public void onResponse(UserBean userBean) {
            if (userBean.getSuccess().equals("0")){
                Toast.makeText(FoodDetailActivity.this,"收藏失败",Toast.LENGTH_SHORT).show();
            }
            else {
                if (isCollect){
                    isCollect=false;
                    iv_collect.setImageResource(R.drawable.collect1);
                    tv_collect.setText("收藏");
                    Toast.makeText(FoodDetailActivity.this,"取消收藏成功",Toast.LENGTH_SHORT).show();
                }
                else{
                    isCollect=true;
                    iv_collect.setImageResource(R.drawable.s7);
                    tv_collect.setText("取消收藏");
                    Toast.makeText(FoodDetailActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
                }
            }
        }

        @Override
        public void onFail(String mag) {
            Toast.makeText(FoodDetailActivity.this,mag,Toast.LENGTH_SHORT).show();

        }
    };

    private IsCollectListener isCollectListener=new IsCollectListener() {
        @Override
        public void onRespose(UserBean userBean) {
            if(userBean.getCollected().equals("0")){
                isCollect=false;
                iv_collect.setImageResource(R.drawable.collect1);
                tv_collect.setText("收藏");
            }
            else{
                isCollect=true;
                iv_collect.setImageResource(R.drawable.s7);
                tv_collect.setText("取消收藏");
            }
        }

        @Override
        public void onFail(String mag) {
            Toast.makeText(FoodDetailActivity.this,mag,Toast.LENGTH_SHORT).show();
        }
    };
}
